package 数组.最小索引总和;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/7/24 22:17
 * @description:
 * @version:
 */
public class solution {
    public static void main(String[] args) {
        String[] list1 = {"Shogun", "Tapioca Express", "Burger King", "KFC"};
        String[] list2 = {"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"};
        findRestaurant(list1,list2);
    }

    public static String[] findRestaurant(String[] list1, String[] list2) {
        Stack<String> stack = new Stack<>();
        Map<String,Integer> map1 = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            map1.put(list1[i],i);
        }
        Map<String,Integer> map2 = new HashMap<>();
        for (int i = 0; i < list2.length; i++) {
            map2.put(list2[i],i);
        }
        List<Map.Entry<String, Integer>> collect = map1.entrySet().stream().filter(x -> {
            if (map2.containsKey(x.getKey())) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        final int[] min = {Integer.MAX_VALUE};
        collect.forEach(item -> {
            int tmp = item.getValue() + map2.get(item.getKey());
            if (min[0] == tmp) {
                stack.push(item.getKey());
            } else if (min[0] > tmp) {
                min[0] = tmp;
                stack.clear();
                stack.push(item.getKey());
            }
        });
        String[] res = new String[stack.size()];
        int i = 0;
        while (!stack.isEmpty()) {
            res[i] = stack.pop();
            i++;
        }
        return res;
    }



    //TODO 基本思想和自己写的差不多，这个更加简便
    public String[] findRestaurant2(String[] list1, String[] list2) {
        int n = list1.length, m = list2.length;
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) map.put(list1[i], i);
        List<String> ans = new ArrayList<>();
        int min = 3000;
        for (int i = 0; i < m; i++) {
            String s = list2[i];
            if (!map.containsKey(s)) continue;
            if (i + map.get(s) < min) {
                ans.clear();
                min = i + map.get(s);
                ans.add(s);
            } else if (i + map.get(s) == min) {
                ans.add(s);
            }
        }
        return ans.toArray(new String[ans.size()]);
    }


    //根据go版本进化而来
    public static String[] findRestaurant3(String[] list1, String[] list2) {
        Map<String,Integer> map1 = new HashMap<>();
        Map<String,Integer> map2 = new HashMap<>();
        for (int i = 0; i < list1.length; i++) {
            map1.put(list1[i],i);
        }
        for (int i = 0; i < list2.length; i++) {
            map2.put(list2[i],i);
        }
        List<String> res = new ArrayList<>();
        int count = Integer.MAX_VALUE;
        List<Map.Entry<String, Integer>> collect = map1.entrySet().stream().collect(Collectors.toList());

        for (int i = 0; i < collect.size(); i++) {
            Map.Entry<String, Integer> item = collect.get(i);
            if (map2.containsKey(item.getKey())) {
                int tmp = item.getValue() + map2.get(item.getKey());
                if (tmp < count) {
                    res.clear();
                    res.add(item.getKey());
                    count = tmp;
                } else if (tmp == count) {
                    res.add(item.getKey());
                    count = tmp;
                }
            }
        }
        return res.toArray(new String[0]);
    }
}
